
package kaizen.jdbc;

import java.io.IOException;
import java.io.PrintStream;

import java.util.Properties;
import java.util.Vector;

import javax.servlet.http.HttpServlet;

import kaizen.nomweb.Historico;
import kaizen.nomweb.utilerias;

// Referenced classes of package kaizen.jdbc:
//            cMgr

public final class DbAppCache extends HttpServlet 
{

  public DbAppCache()
  {
  }

  public static void borrarCache()
  {
    queries.removeAllElements();
    nombres.removeAllElements();
  }

  public static Vector execQuery(String sSql) throws IOException
  {
    if (!binicio)
      init1();
    boolean encontrado = false;
    int poss = 0;
    int i = 0;
    Vector vRecord = null;
    if (nombres == null)
    {
      nombres = new Vector();
      queries = new Vector();
    }
    if (nombres.size() > 0)
      for (; i < nombres.size(); i++)
      {
        String compara = (String)nombres.elementAt(i);
        if (compara.equals(sSql))
        {
          encontrado = true;
          poss = i;
          i = nombres.size();
        }
      }

    if (encontrado)
    {
      vRecord = (Vector)queries.elementAt(poss);
    } else
    {
      String conn = nombre_conn + "_" + String.valueOf(i_query);
      Properties prop = new Properties();
      prop = utilerias.loadProperties("/kaizen/general.properties");
      String ls_driver = prop.getProperty("driverCache");
      String ls_url = prop.getProperty("urlCache");
      String ls_user = prop.getProperty("userCache");
      String ls_password = prop.getProperty("passwordCache");
      boolean bValor = cMgr.add(conn, ls_driver, ls_url, ls_user, ls_password);
      String arch_log = prop.getProperty("log_Cache_Query");
      Historico log = new Historico(arch_log, true);
      log.agregar(utilerias.fechaHoy("MMMMM.dd GGG hh:mm aaa") + ", " + 
                  String.valueOf(i_query) + " : " + sSql);
      log.cerrar();
      i_query++;
      String cursor = "query_" + String.valueOf(i_query);
      cMgr.executeQuery(conn, cursor, sSql);
      vRecord = cMgr.getAllRecords(conn, cursor);
      queries.addElement(vRecord);
      nombres.addElement(sSql);
      cMgr.close(conn);
    }
    return vRecord;
  }

  public static void init1()
  {
    queries = new Vector();
    nombres = new Vector();
    i_query = 0;
    binicio = true;
  }

  public static Vector queries = null;
  public static Vector nombres = null;
  public static int i_query;
  private static String nombre_conn = "exec_cache";
  private static boolean binicio = false;

}
